package ModelLayer;
import java.util.*;
import java.text.*;

public class Sale{
    private int id;
    private String date;
    private int productBarcode;
    private int amount;
    private int employeeID;
    private int customerID;
    private boolean ready;
    private double totalPrice;
    private HashMap<Integer, Integer> products;

    public Sale(int id, int employeeID, int customerID){
        this.id = id;
        this.employeeID = employeeID;
        this.customerID = customerID;
        this.ready = false;
        date = getSaleDate();
        products = new HashMap<Integer,Integer>();
    }
    
    private String getSaleDate()
    {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
        Date date = new Date();
        String dateString = dateFormat.format(date);
        return dateString;
    }

    public void addProduct(int productBarcode, int amount){
        if(products.containsKey(productBarcode)) {
            int modification = amount;
            modification += products.get(productBarcode);
            products.remove(productBarcode);
            products.put(productBarcode,modification);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setSold(amount);
            CustomerContainer.getInstance().findCustomerById(customerID).setBought(amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setSold(amount);
        } else {
            products.put(productBarcode, amount);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setSold(amount);
            CustomerContainer.getInstance().findCustomerById(customerID).setBought(amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setSold(amount);
        }
        
        if(amount >= ProductContainer.getInstance().findProductByBarcode(productBarcode).getAmountForDiscount()) {
            double addPrice = 0.0;
            double price = ProductContainer.getInstance().findProductByBarcode(productBarcode).getPriceForDiscount();
            price = price * amount;
            addPrice = price;
            price = CustomerContainer.getInstance().findCustomerById(customerID).getDiscount();
            price = addPrice * (price / 100);
            addPrice -= price;
            totalPrice += addPrice;
            CustomerContainer.getInstance().findCustomerById(customerID).setIncome(addPrice);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setIncome(addPrice);
        } else {
            double addPrice = 0.0;
            double price = ProductContainer.getInstance().findProductByBarcode(productBarcode).getSellingPrice();
            price = price * amount;
            addPrice = price;
            price = CustomerContainer.getInstance().findCustomerById(customerID).getDiscount();
            price = addPrice * (price / 100);
            addPrice -= price;
            totalPrice += addPrice;
            CustomerContainer.getInstance().findCustomerById(customerID).setIncome(addPrice);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setIncome(addPrice);
        }
    }
    
    public double getTotalPrice()
    {
        return totalPrice;
    }

    public void removeProduct(int productBarcode,int amount){
        if(products.containsKey(productBarcode) && products.get(productBarcode) > amount){
            int modification = products.get(productBarcode) - amount;
            products.remove(productBarcode);
            products.put(productBarcode, modification);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setSold(-amount);
            CustomerContainer.getInstance().findCustomerById(customerID).setBought(-amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setSold(-amount);
            double removePrice = 0.0;
            double price = ProductContainer.getInstance().findProductByBarcode(productBarcode).getSellingPrice();
            price = price * amount;
            removePrice = price;
            price = CustomerContainer.getInstance().findCustomerById(customerID).getDiscount();
            price = removePrice * (price / 100);
            removePrice -= price;
            totalPrice -= removePrice;
            CustomerContainer.getInstance().findCustomerById(customerID).setIncome(-removePrice);
            EmployeeContainer.getInstance().findEmployeeById(employeeID).setIncome(-removePrice);
        }
    }
    
    public boolean isReady() {
        return ready;
    }

    public void setReady() {
        if(totalPrice == 0) {
            ready = false;
        } else {
            ready = true;
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public String getDate()
    {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getEmployeeID() {
        return employeeID;
    }

    public void setEmployeeID(int employeeID) {
        this.employeeID = employeeID;
    }

    public int getCustomerID() {
        return customerID;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }
    
    public HashMap<Integer,Integer> getProducts()
    {
        return products;
    }
}